package smartcounters.androidclient;

import smartcounters.androidclient.WebServices.ConnectionError;
import smartcounters.androidclient.WebServices.InformationReaderWebServiceClient;
import smartcounters.androidclient.WebServices.OnResultReceiver;
import smartcounters.androidclient.model.AuthenticatedUserCredentials;
import smartcounters.androidclient.model.Configuration;
import smartcounters.androidclient.model.UserInfo;
import smartcounters.androidclient.model.eOperationsEnum;
import smartcounters.androidclient.model.eUserPermissions;
import android.os.Bundle;
import android.os.Message;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;

public class MainActivity extends Activity implements OnResultReceiver, OnClickListener {
	private Button m_AccountsButton;
	private Button m_UsersButton;
	private Button m_FaultsAndAccountsButton;
	private Button m_SendFaultButton;
	private Button m_MyBillsButton;
	private Button m_MyUserInformationButton;
	private View m_SeparatorView;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_layout);
        
        InformationReaderWebServiceClient client = new InformationReaderWebServiceClient(Configuration.getInstance().getServerAddress());
		client.GetUserInformationByID(new AuthenticatedUserCredentials(1, "12345678"), Configuration.getInstance().getUserCredentials().getID());
		client.RegisterResultReceiver(this);
    }

	public void ReceiveResult(Message i_Msg) {
		if (i_Msg.obj != null){
           	if (!(i_Msg.obj instanceof ConnectionError)){
           		if (eOperationsEnum.values()[i_Msg.what] == eOperationsEnum.GetUserInformationByID){
        			UserInfo userInfo = (UserInfo) i_Msg.obj;
        			
        			if (userInfo.getUserPermissions().contains(eUserPermissions.All)){
        				// Admin => Show everything in the menu.
        				
        				LinearLayout layout = (LinearLayout)findViewById(R.id.mainLayout);
        				
        				m_AccountsButton = new Button(this);
        				m_AccountsButton.setText("Accounts Management");
        				m_AccountsButton.setOnClickListener(this);
        				layout.addView(m_AccountsButton);
        				
        				m_UsersButton = new Button(this);
        				m_UsersButton.setText("Users Management");
        				m_UsersButton.setOnClickListener(this);
        				layout.addView(m_UsersButton);
        				
        				m_FaultsAndAccountsButton = new Button(this);
        				m_FaultsAndAccountsButton.setText("Faults and Accounts (GIS)");
        				m_FaultsAndAccountsButton.setOnClickListener(this);
        				layout.addView(m_FaultsAndAccountsButton);
        				
        				m_SeparatorView = new View(this);
        				LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, 1);
        				params.setMargins(0, 25, 0, 0);
        				m_SeparatorView.setLayoutParams(params);
        				m_SeparatorView.setBackgroundColor(0x0909090);
        				layout.addView(m_SeparatorView);
        			}
        			
        			// The rest of the buttons are available for everyone.
        			
    				LinearLayout layout = (LinearLayout)findViewById(R.id.mainLayout);
    				
    				m_MyUserInformationButton = new Button(this);
    				m_MyUserInformationButton.setText("My User Information");
    				m_MyUserInformationButton.setOnClickListener(this);
    				layout.addView(m_MyUserInformationButton);
    				
    				m_SendFaultButton = new Button(this);
    				m_SendFaultButton.setText("Send Fault Report");
    				m_SendFaultButton.setOnClickListener(this);
    				layout.addView(m_SendFaultButton);
    				
    				m_MyBillsButton = new Button(this);
    				m_MyBillsButton.setText("My Bills");
    				m_MyBillsButton.setOnClickListener(this);
    				layout.addView(m_MyBillsButton);
        		}
        	}
        }	
	}

	public void onClick(View arg0) {
		 if (arg0 == m_AccountsButton) {
			 Intent intent = new Intent();
			 intent.setClass(this, AccountsActivity.class);
			 startActivity(intent);
		 }
		 else if (arg0 == m_UsersButton) {
			 Intent intent = new Intent();
			 intent.setClass(this, UsersActivity.class);
			 startActivity(intent);
		 }
		 else if (arg0 == m_MyUserInformationButton) {
			 Intent intent = new Intent();
			 intent.setClass(this, MyUserInfoActivity.class);
			 startActivity(intent);
		 }
		 else if (arg0 == m_SendFaultButton) {
			 Intent intent = new Intent();
			 intent.setClass(this, SendFaultActivity.class);
			 startActivity(intent);
		 }
		 else if (arg0 == m_MyBillsButton) {
			 Intent intent = new Intent();
			 intent.setClass(this, MyBillsActivity.class);
			 startActivity(intent);
		 }
		 else if (arg0 == m_FaultsAndAccountsButton) {
			 Intent intent = new Intent();
			 intent.setClass(this, FaultsAndAccountsActivity.class);
			 startActivity(intent);
		 }
	}
}